Ext.define("App.datasource.view.DataTableEditor", {
    extend: 'Extend.window.FormWindow',
    alias: 'widget.DataTableEditor',
    entity: 'entity',
    requires: ['App.datasource.field.TableFieldGridField', 'App.datasource.model.TableFieldModel'],
    resetBySave: false,
    listeners: {},
    config: {
        window: {
            height: 600,
            width: 1000
        }
    },
    defaults: {
        columnWidth: .4,
        maxLength: 128,
        labelWidth: 70
    },
    items: [{
        xtype: 'hidden',
        name: 'id'
    }, {
        xtype: 'hidden',
        name: 'creator'
    }, {
        xtype: 'textfield',
        name: 'alias',
        fieldLabel: '数据名称',
        maxLength: 128,
        columnWidth: 0.3,
        allowBlank: false,
        blankText: '表为必填字段，不能为空！',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>'],
    }, {
        xtype: 'textfield',
        fieldLabel: '表',
        name: 'tableName',
        maxLength: 128,
        allowBlank: false,
        blankText: '表为必填字段，不能为空！',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>'],
        columnWidth: 0.3,
        labelWidth: 30
    }, {
        xtype: 'booleancombobox',
        fieldLabel: '虚拟表',
        name: 'virtualTable',
        allowBlank: false,
        blankText: '虚拟表为必填字段，不能为空！',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>'],
        columnWidth: 0.2,
        labelWidth: 60
    }, {
        xtype: 'combobox',
        editable: false,
        queryMode: 'local',
        displayField: 'name',
        valueField: 'value',
        store: {data: [{value: 'project', name: '项目'}, {value: 'iteration', name: '迭代'}]},
        fieldLabel: '填报周期',
        name: 'type',
        allowBlank: false,
        blankText: '填报周期为必填字段，不能为空！',
        beforeLabelTextTpl: ['<span style="color:red;">*</span>'],
        columnWidth: 0.2
    }, {
        xtype: 'textfield',
        fieldLabel: '描述',
        name: 'description',
        columnWidth: 1
    }, {
        xtype: 'TableFieldGridField',
        fieldLabel: '字段配置',
        name: 'tableFields',
        submitFields: ['id', 'alias', 'fieldName', 'conditions', 'dataType','readOnly', {column: 'index', field: 'idx'}],
        columnWidth: 1,
        height: 380,
        model: 'App.datasource.model.TableFieldModel',
        deleteHandler: function (rs) {
            var me = this;
            var ids = me.getSelectedIds();
            if (ids.length > 0) {
                me.mask('检查引用状态...')
                Ext.Ajax.request({
                    url: 'kpiconfiguration/query',
                    params: {'params.tableFields.id_in': ids},
                    success: function (resp) {
                        if (resp.result.data.length > 0) {
                            Extend.Msg.error('错误', '部份数据字段正在使用中，请检查并移除引用后重试！');
                        } else {
                            me.getStore().remove(rs);
                            me.deselectAll();
                        }
                        me.unmask();
                    },
                    failure: function () {
                        me.unmask();
                    }
                });
            }
        }
    }],
    isValid: function () {
        var valid = this.callParent(arguments);
        if (valid == true) {
            var store = this.down('TableFieldGridField').getStore();
            for (var i = 0; i < store.getCount(); i++) {
                var record = store.getAt(i);
                var validation = record.getValidation();
                var valid = validation.isValid();
                if (valid == false) {
                    this.down('TableFieldGridField').findPlugin('extendrowediting').startEdit(record, 1);
                    return false;
                }
            }
        }
        return valid;
    }
})
